home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Games / MoofWars / MoofEncoder / main.cp < prev    next >
Encoding:
Text File  |  2000-09-28  |  8.7 KB  |  392 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        main.cp
  3.  
  4.     Contains:    An extremely basic encoder utility to assist in building all of the various
  5.                 "compiled" resources used by MoofWars.  Many of the encoder utilities are 
  6.                 actually handled by things in the standard TGraphic class, so the main reason
  7.                 for pre-encoding the graphics is to improve the time to load the graphics in 
  8.                 the game.
  9.  
  10.     Written by: Timothy Carroll    
  11.  
  12.     Copyright:    Copyright © 1996-1999 by Apple Computer, Inc., All Rights Reserved.
  13.  
  14.                 You may incorporate this Apple sample source code into your program(s) without
  15.                 restriction. This Apple sample source code has been provided "AS IS" and the
  16.                 responsibility for its operation is yours. You are not permitted to redistribute
  17.                 this Apple sample source code as "Apple sample source code" after having made
  18.                 changes. If you're going to re-distribute the source, we require that you make
  19.                 it clear in the source that the code was descended from Apple sample source
  20.                 code, but that you've made changes.
  21.  
  22.     Change History (most recent first):
  23.                 7/1/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  24.                 2/23/98     Timothy Carroll    Updated menu calls to latest Universal Headers
  25.                 2/27/97        Timothy Carroll    Now explicitly include Main.h
  26.                 8/15/96        Timothy Carroll Initial Release
  27.                 
  28.     
  29.                 
  30.  
  31. */
  32.  
  33. #include <Fonts.h>
  34. #include <Devices.h>
  35. #include <Resources.h>
  36. #include <StandardFile.h>
  37.  
  38. #include "Main.h"
  39. #include "GridEncode.h"
  40. #include "ICL8Encode.h"
  41. #include "PictEncode.h"
  42. #include "PICTMask.h"
  43.  
  44. /*********************************************************************************
  45.     Globals    
  46. *********************************************************************************/
  47.  
  48. CTabHandle            gAppColorTable = NULL;
  49. static Boolean         gDone = false;
  50.  
  51.  
  52. /*********************************************************************************
  53.     Functions    
  54. *********************************************************************************/
  55.  
  56. void main (void);
  57.  
  58. OSStatus InitApp (void);
  59. OSStatus RunApp (void);
  60. OSStatus QuitApp (void);
  61. OSStatus HandleMenuCommand (SInt16 theMenu, SInt16 theItem);
  62. OSStatus GetFiles (SInt16 *input, SInt16 *output);
  63.  
  64.  
  65.  
  66.  
  67.  
  68. void main (void)
  69. {
  70.     OSStatus theErr = noErr;
  71.     int loop;
  72.     
  73.     // Standard Mac Init
  74.     InitGraf(&qd.thePort);
  75.     InitFonts();
  76.     InitWindows();
  77.     InitMenus();
  78.     TEInit();
  79.     InitDialogs(nil);
  80.     InitCursor();
  81.  
  82.     MaxApplZone();
  83.     
  84.     for (loop = 0; loop < kNumberOfMasters; loop++)
  85.         MoreMasters();
  86.  
  87.     // Initialize Application Specifics
  88.     theErr = InitApp();
  89.     FAIL_OSERR (theErr, "\pFailed to initialize the application")
  90.  
  91.     // Run the eventloop
  92.     theErr = RunApp();
  93.     
  94.     error:
  95.     // On an error, the application will just quit.  Probably would be nice in a real
  96.     // app to put up a dialog box or something.
  97.     
  98.     theErr = QuitApp();
  99. }
  100.  
  101. OSStatus InitApp (void)
  102. {
  103.     OSStatus    theErr         = noErr;
  104.     Handle        menuBarH     = NULL;
  105.     MenuRef        appleMenuH     = NULL;
  106.     MenuRef        engineMenuH    = NULL;
  107.     
  108.     // read and install the menu bar, adding DA names.
  109.     menuBarH = GetNewMBar(rMenuBar);
  110.     FAIL_NIL (menuBarH, "\pFailed to load the MenuBar")
  111.  
  112.     SetMenuBar(menuBarH);
  113.     DisposeHandle(menuBarH);
  114.     
  115.     appleMenuH = GetMenuHandle(mAppleMenu);
  116.     FAIL_NIL (appleMenuH, "\pFailed to find the Apple Menu")
  117.     AppendResMenu(appleMenuH, 'DRVR');
  118.     
  119.     DrawMenuBar();
  120.      
  121.      goto cleanup;
  122.      
  123.     error:
  124.     
  125.     if (theErr == noErr)
  126.         theErr = paramErr;
  127.     
  128.     cleanup:
  129.     return theErr;
  130. }
  131.  
  132.  
  133. OSStatus RunApp (void)
  134. {
  135.  
  136.     WindowRef    whichWindow = NULL;
  137.     OSStatus    theErr = noErr;
  138.     
  139.     EventRecord    theEvent;
  140.     short         partCode;
  141.     long        menuResult;
  142.     short        theMenu, theItem;
  143.  
  144.     while (!gDone)
  145.     {
  146.         (void) WaitNextEvent(everyEvent,&theEvent,kSleepTime,NULL);
  147.  
  148.         switch (theEvent.what)
  149.         {
  150.             case    nullEvent:
  151.                 break;            
  152.             case    mouseDown:
  153.                 partCode = FindWindow(theEvent.where,&whichWindow);
  154.             
  155.                 switch(partCode)
  156.                 {
  157.                     case    inMenuBar:
  158.                         menuResult = MenuSelect(theEvent.where);
  159.                         theMenu = (menuResult >> 16) & 0x0000FFFF;
  160.                         theItem = (menuResult) & 0x0000FFFF;
  161.                         
  162.                         HandleMenuCommand (theMenu, theItem);
  163.                         HiliteMenu(0);
  164.  
  165.                         break;
  166.  
  167.                     default:
  168.                         break;
  169.                     }
  170.                 break;
  171.         
  172.             case    keyDown:
  173.                 if (theEvent.modifiers & cmdKey)
  174.                     {
  175.                         menuResult = MenuKey((short) theEvent.message & charCodeMask);
  176.                         theMenu = (menuResult >> 16) & 0x0000FFFF;
  177.                         theItem = (menuResult) & 0x0000FFFF;
  178.                     
  179.                         HandleMenuCommand (theMenu, theItem);
  180.                         
  181.                         HiliteMenu(0);
  182.                     }
  183.                 break;
  184.             default:
  185.                 break;
  186.         }
  187.  
  188.     }
  189.  
  190.     return noErr;
  191. }
  192.  
  193.  
  194.  
  195. OSStatus QuitApp (void)
  196. {
  197.     return noErr;
  198. }
  199.  
  200. OSStatus HandleMenuCommand (SInt16 theMenu, SInt16 theItem)
  201. {
  202.     OSStatus theErr = noErr;
  203.  
  204.     switch (theMenu)
  205.     {
  206.         case mAppleMenu:
  207.             if (theItem == iAboutApp)
  208.                 (void) Alert (128, NULL); 
  209.             else
  210.                 {
  211.                 MenuRef        appleMenuH     = NULL;
  212.                 Str255        deskAccName;
  213.                 
  214.                 appleMenuH = GetMenuHandle(mAppleMenu);
  215.                 FAIL_NIL (appleMenuH, "\pFailed to find the Apple Menu")
  216.                 
  217.                 GetMenuItemText(appleMenuH,theItem,deskAccName);
  218.                 (void) OpenDeskAcc(deskAccName);
  219.                 }
  220.                 
  221.         break;
  222.         
  223.         case mFileMenu:
  224.             switch (theItem)
  225.             {
  226.                 case iEncodeIcons:
  227.                 {
  228.                     SInt16 in, out;
  229.                     theErr = GetFiles (&in, &out);
  230.                     FAIL_OSERR (theErr, "\p Failed to open the files")
  231.                     
  232.                     ICL8Encode (in, out);
  233.     
  234.                     CloseResFile(in);
  235.                     CloseResFile (out);
  236.                 }
  237.                 break;
  238.                 
  239.                 case iEncodePICTs:
  240.                 {
  241.                     SInt16 in, out;
  242.                     theErr = GetFiles (&in, &out);
  243.                     FAIL_OSERR (theErr, "\p Failed to open the files")
  244.                     
  245.                     PICTEncode (in, out);
  246.     
  247.                     CloseResFile(in);
  248.                     CloseResFile (out);
  249.                 }
  250.                 break;
  251.                 
  252.                 case iTriplePICTs:
  253.                 {
  254.                     SInt16 in, out;
  255.                     theErr = GetFiles (&in, &out);
  256.                     FAIL_OSERR (theErr, "\p Failed to open the files")
  257.                     
  258.                     GeneratePICTMasks (in, out);
  259.     
  260.                     CloseResFile(in);
  261.                     CloseResFile (out);
  262.                 }
  263.                 break;
  264.                 
  265.                 case iEncodeTiles:
  266.                 {
  267.                     SInt16 in, out;
  268.                     theErr = GetFiles (&in, &out);
  269.                     FAIL_OSERR (theErr, "\p Failed to open the files")
  270.                     
  271.                     GridTileEncode (in, out);
  272.     
  273.                     CloseResFile(in);
  274.                     CloseResFile (out);
  275.                 }
  276.                 break;
  277.                 
  278.                 case iQuit:
  279.                     gDone = true;
  280.                 break;
  281.             }
  282.     }
  283.     return noErr;
  284.     
  285.     error:
  286.     if (theErr == noErr)
  287.         theErr = paramErr;
  288.     return theErr;
  289. }
  290.  
  291.  
  292. OSStatus GetFiles (SInt16 *input, SInt16 *output)
  293. {
  294.     // This routine displays the standard get and put file dialogs so that the user
  295.     // can select a file to read from, and to create an output file.
  296.     
  297.     StandardFileReply        inputFile;
  298.     StandardFileReply        outputFile;
  299.     SFTypeList                typeList;
  300.     Point                     p;
  301.     short                    inResNum, outResNum;
  302.  
  303.     typeList[0] = 'RSRC';
  304.     typeList[1] = 'rsrc';
  305.     
  306.     p.h = 100; p.v = 100;
  307.     StandardGetFile (NULL, 2, typeList, &inputFile);
  308.     
  309.     if (!inputFile.sfGood)
  310.         return paramErr;
  311.         
  312.     StandardPutFile ("\pSave file as...", "\pUntitled",&outputFile);
  313.     
  314.     if (!(outputFile.sfGood && !outputFile.sfReplacing))
  315.         return paramErr;
  316.     
  317.     inResNum = FSpOpenResFile (&inputFile.sfFile, fsRdPerm);
  318.     FSpCreateResFile(&outputFile.sfFile, 'Doug', 'RSRC', outputFile.sfScript);
  319.     outResNum = FSpOpenResFile (&outputFile.sfFile, fsRdWrPerm);
  320.     
  321.     *input = inResNum;
  322.     *output = outResNum;
  323.     return noErr;
  324. }
  325.  
  326. // This copies a particular resource from the source res file to the destination.
  327. OSStatus CopyResource (SInt16 inRes, SInt16 outRes, OSType theType, SInt16 theID)
  328. {
  329.     OSStatus    theErr = noErr;
  330.     
  331.     Handle        temp = NULL;
  332.     
  333.     // temp can be either a resource or a handle, we use this to tell what the current status is, so
  334.     // that if we need to cleanup, we can do this correctly.
  335.     Boolean        tempIsResource = true; 
  336.     
  337.     // we pass these to GetResInfo so that we can get the actual resource ID.
  338.     SInt16        resID;
  339.     ResType        resType;
  340.     Str255        resName;
  341.  
  342.     SInt16        saveResNum; // Used to save the resource chain info
  343.     
  344.     saveResNum = CurResFile();
  345.  
  346.     UseResFile (inRes);
  347.  
  348.     // attempt to grab the color table resource.  We grab it as a resource because we're going
  349.     // to write it right back out into the output file.
  350.  
  351.     temp = Get1Resource (theType, theID);
  352.     theErr = ResError();
  353.     FAIL_NIL (temp, "\pFailed to load the resource")
  354.     FAIL_OSERR (theErr, "\pFailed to load the resource")
  355.     
  356.     GetResInfo( temp, &resID, &resType, resName ); // we do this just to get the name
  357.     theErr = ResError();
  358.     FAIL_OSERR( theErr, "\pFailed to get info on the resource")
  359.     
  360.     DetachResource (temp);
  361.     theErr = ResError();
  362.     FAIL_OSERR( theErr, "\pFailed to detach the resource from the input chain")
  363.     
  364.     tempIsResource = false;
  365.         
  366.     UseResFile (outRes);
  367.     
  368.     AddResource( temp, theType, theID, resName );
  369.     theErr = ResError();
  370.     FAIL_OSERR (theErr, "\pFailed to add the resource to the file")
  371.     
  372.     tempIsResource = true;
  373.     
  374.     WriteResource( temp );
  375.     theErr = ResError();
  376.     FAIL_OSERR (theErr, "\pFailed to write the resource to the file")
  377.  
  378.     goto cleanup;
  379.     
  380. error:
  381.     if (theErr == noErr)
  382.         theErr = paramErr;
  383. cleanup:
  384.     if (temp != NULL)
  385.         if (tempIsResource)
  386.             ReleaseResource (temp);
  387.         else
  388.             DisposeHandle (temp);
  389.     
  390.     UseResFile (saveResNum);
  391.     return theErr;
  392. }